//
//  USMemorialPreviewVC.m
//  us
//
//  Created by karry on 2025/5/13.
//

#import "USMemorialPreviewVC.h"
#import "USImageActivityItem.h"

@interface USMemorialPreviewVC ()

@end

@implementation USMemorialPreviewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"预览";
    [self setupView];
    
    [self loadData];
}
 

- (void)setupView {
    [self.contentView jk_shadowWithColor:UIColor.lightGrayColor offset:CGSizeMake(3, 3) opacity:0.2 radius:3];

    self.leftCharacterView.character = USUser.user.character;
    self.rightCharacterView.character = USUser.user.relCharacter;

    self.coverImageView.hidden = YES;
        
    UIButton *btn0 = [UIButton us_fillButtonWithTitle:@"下载"];
    [btn0 addTarget:self action:@selector(downLoadAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn0];
    
    UIButton *btn1 = [UIButton us_fillButtonWithTitle:@"分享"];
    [btn1 addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    [btn0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.height.mas_equalTo(UIButton.us_buttonDefaultHeight);
        make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom).mas_offset(-20);
    }];
    
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(btn0.mas_right).mas_offset(20);
        make.right.mas_offset(-20);
        make.width.height.centerY.mas_equalTo(btn0);
        
    }];
    
    
    UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"贴纸",@"人物"]];
    seg.selectedSegmentTintColor = [UIColor mi_1_color];
    [seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:seg];
    [seg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.bottom.mas_equalTo(btn1.mas_top).mas_offset(-16);
    }];
    
    seg.selectedSegmentIndex = 1;
    
    self.backgroundView.backgroundColor = [UIColor clearColor];
    self.backgroundView.layer.cornerRadius = 14.f;
    self.backgroundView.layer.masksToBounds = YES;
    self.backgroundView.backgroundColor = [UIColor mi_1_color];
}

- (void)segAction:(UISegmentedControl *)sender{
    [USFeedbackTool selectionChanged];
    
    if (sender.selectedSegmentIndex == 0) {
        self.leftCharacterView.hidden = YES;
        self.rightCharacterView.hidden = YES;

        self.coverImageView.hidden = NO;
    }else{
        self.leftCharacterView.hidden = NO;
        self.rightCharacterView.hidden = NO;

        self.coverImageView.hidden = YES;
    }
}


- (void)loadData {
    [super loadData];
    
    self.titleLab.text = self.dayModel.anniversaryName;
    
    self.infoLab.text = self.dayModel.dayDescription;
    
    UIColor *color = [UIColor jk_colorWithHexString:self.dayModel.color];
    self.topView.backgroundColor = [color colorWithAlphaComponent:0.5];
    
    if (self.dayModel.coverUrl.length == 0) {
        self.coverImageView.image = [UIImage systemImageNamed:@"circle.fill"];
    }else{
        self.coverImageView.image = [UIImage imageNamed:self.dayModel.coverUrl];
    }
    
    self.coverImageView.tintColor = color;
    self.titleLab.adjustsFontSizeToFitWidth = YES;
    self.titleLab.textColor = [color jk_darkenColor:0.35];
    
    self.contentLab.attributedText = [self.dayModel dayAttributeWithFontSize:52];
 

}


- (void)downLoadAction:(UIButton *)sender{
    [USFeedbackTool impactSoft];
    
    UIImage *image = [self.contentView jk_screenshot];
    if (image) {
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    }
    
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        NSLog(@"保存失败: %@", error.localizedDescription);
        [self.view makeToast:@"保存失败"];
    } else {
        NSLog(@"保存成功");
        [self.view makeToast:@"已保存到相册"];
    }
}


- (void)shareAction:(id)sender{
    
    [USFeedbackTool impactSoft];

    UIImage *image = [self.contentView jk_screenshot];
    if (!image) return;
    
    USImageActivityItem *item = [[USImageActivityItem alloc]initWithImage:image];
    item.title = @"纪念日";
    
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[item] applicationActivities:nil];
    activityVC.popoverPresentationController.sourceView = sender;
    [self presentViewController:activityVC animated:YES completion:nil];
    
}

@end
